import 'package:flutter/material.dart';
import '../components/custom_navbar.dart';
import '../components/pull_refresh_wrapper.dart';
import '../components/base_list_controller.dart';

/// 示例数据模型
class ExampleItem {
  final int id;
  final String title;
  final String description;

  ExampleItem({
    required this.id,
    required this.title,
    required this.description,
  });
}

/// 示例列表控制器
class ExampleListController extends BaseListController<ExampleItem> {
  @override
  Future<List<ExampleItem>> loadData({required int page, required int pageSize}) async {
    // 模拟网络请求延迟
    await Future.delayed(const Duration(seconds: 1));
    
    // 模拟数据生成
    final List<ExampleItem> data = [];
    final startId = (page - 1) * pageSize + 1;
    
    // 生成模拟数据，限制最大页数为5
    final endId = page < 5 ? startId + pageSize - 1 : startId + 10; // 第5页只有10条数据
    
    for (int i = startId; i <= endId; i++) {
      data.add(ExampleItem(
        id: i,
        title: '项目 $i',
        description: '这是项目 $i 的详细描述信息，用于演示下拉刷新和上拉加载功能。',
      ));
    }
    
    return data;
  }
}

class PullRefreshExamplePage extends StatefulWidget {
  const PullRefreshExamplePage({Key? key}) : super(key: key);

  @override
  State<PullRefreshExamplePage> createState() => _PullRefreshExamplePageState();
}

class _PullRefreshExamplePageState extends State<PullRefreshExamplePage> {
  final ExampleListController _controller = ExampleListController();
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();
    // 初始化数据
    _initializeData();
  }

  Future<void> _initializeData() async {
    await _controller.initData();
    setState(() {
      _isInitialized = true;
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: Column(
        children: [
          // 导航栏
          CustomNavbar(
            title: '下拉刷新示例',
            showBackButton: true,
          ),
          
          // 列表内容
          Expanded(
            child: !_isInitialized
                ? const Center(child: CircularProgressIndicator())
                : PullRefreshWrapper(
                    controller: _controller.refreshController,
                    onRefresh: () async {
                      await _controller.refreshData();
                      setState(() {});
                    },
                    onLoadMore: () async {
                      await _controller.loadMoreData();
                      setState(() {});
                    },
                    enableLoadMore: _controller.hasMore,
                    isEmpty: _controller.items.isEmpty,
                    emptyWidget: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.inbox, size: 64.0, color: Colors.grey[400]),
                          const SizedBox(height: 16.0),
                          Text('暂无数据', style: TextStyle(color: Colors.grey[600])),
                          ElevatedButton(
                            onPressed: _initializeData,
                            child: const Text('重新加载'),
                          ),
                        ],
                      ),
                    ),
                    child: ListView.builder(
                      itemCount: _controller.items.length,
                      itemBuilder: (context, index) {
                        final item = _controller.items[index];
                        return Card(
                          margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                          child: Padding(
                            padding: const EdgeInsets.all(16.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  item.title,
                                  style: const TextStyle(
                                    fontSize: 16.0,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                const SizedBox(height: 8.0),
                                Text(
                                  item.description,
                                  style: TextStyle(
                                    fontSize: 14.0,
                                    color: Colors.grey[600],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
          ),
        ],
      ),
    );
  }
}